import 'package:flutter/material.dart';

import '../home/home.dart';
import '../search/search.dart';
import '../publish/publish.dart';
import '../message/message.dart';
import '../user/user.dart';

class BoxPage extends StatefulWidget {
  @override
  _BoxPage createState() => _BoxPage();
}

class _BoxPage extends State<BoxPage> {
  int currentIndex = 0;

  final pageController = PageController();

  final items = [
    BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),
    BottomNavigationBarItem(icon: Icon(Icons.search), title: Text('发现')),
    BottomNavigationBarItem(icon: Icon(Icons.music_video), title: Text('发布')),
    BottomNavigationBarItem(icon: Icon(Icons.message), title: Text('消息')),
    BottomNavigationBarItem(icon: Icon(Icons.people), title: Text('我的')),
  ];

  final bodyList = [
    HomePage(),
    SearchPage(),
    PublishPage(),
    MessagePage(),
    UserPage()
  ];

  void onTap(int index) {
    //使用PageView的pageController
    pageController.jumpToPage(index);
  }

  void onPageChanged(int index) {
    setState(() {
      currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //浮动按钮
      floatingActionButton: Container(
        height: 70,
        width: 70,
        padding: EdgeInsets.all(6),
        margin: EdgeInsets.only(top: 0),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(40), //圆框
            color: Colors.white),
        child: FloatingActionButton(
          heroTag: 'boxFloatingActionButton',
          child: Icon(
            Icons.add,
            color: Colors.white,
          ),
          onPressed: () {
            pageController.jumpToPage(2);
          },
          backgroundColor:
              this.currentIndex == 2 ? Colors.blue : Colors.black, //调整选中颜色
        ),
      ),
      //设置floatingActionButton位置
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      bottomNavigationBar: BottomNavigationBar(
        unselectedItemColor: Colors.black,
        items: items,
        currentIndex: currentIndex,
        fixedColor: Colors.blue,
        type: BottomNavigationBarType.fixed,
        onTap: onTap,
      ),
      body: PageView(
        controller: pageController,
        onPageChanged: onPageChanged,
        children: bodyList,
        physics: NeverScrollableScrollPhysics(), // 禁止左右滑动
      ),
    );
  }
//void _onAdd() {}
}
